package com.iamgod.savemoney.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.iamgod.savemoney.base.BaseDao;
import com.iamgod.savemoney.bean.DayPlanBean;
import com.iamgod.savemoney.bean.PlanStatementsBean;
import com.iamgod.savemoney.sys.Config;

/**
 * 计划执行报表
 * @author llj
 *
 */
public class PlanStatementsDao extends BaseDao {
	
	private SQLiteDatabase sdb;
	
	private static String TAG = "PlanStatementsDao";

	public PlanStatementsDao(Context context, String name) {
		super(context, name);
		if(sdb==null){
			sdb = this.getWritableDatabase();
		}
	}
	
	/**
	 * 插入一个计划
	 * @param bean
	 * @return
	 */
	public void insert(PlanStatementsBean bean){
		String insertSql = "insert into "+Config.DataBase.Table_PlanStatements
				+" ("+
				Config.DataBase.Column_Title+","+
				Config.DataBase.Column_PlanPay+","+
				Config.DataBase.Column_ActualPay+","+
				Config.DataBase.Column_Type+","+
				Config.DataBase.Column_TimeFlag+") " +
				"values (?,?,?,?,?)";
		sdb.execSQL(insertSql, 
				new Object[]{
				bean.getTitle(),
				bean.getPlanPay(),
				bean.getActualPay(),
				bean.getType(),
				bean.getTimeFlag()
				});
	}
	
	/**
	 * 更新一个计划
	 * @param bean
	 */
	public void update(PlanStatementsBean bean){
		String updateSql = "update "+Config.DataBase.Table_PlanStatements
				+" set "+Config.DataBase.Column_Title+" = ?,"
				+""+Config.DataBase.Column_PlanPay+" = ?,"
				+""+Config.DataBase.Column_ActualPay+"=?,"
				+""+Config.DataBase.Column_Type+"=?,"
				+""+Config.DataBase.Column_TimeFlag+"=?"
				+" where "+Config.DataBase.Column_Id+" =  ?";
		sdb.execSQL(updateSql,
				new Object[]{
				bean.getTitle(),
				bean.getPlanPay(),
				bean.getActualPay(),
				bean.getType(),
				bean.getTimeFlag(),
				bean.getId()
				});
		
	}
	
	/**
	 * 删除一个计划
	 * @param id
	 */
	public void delete(long id){
		String deleteSql = "delete from "+Config.DataBase.Table_PlanStatements+" where "+Config.DataBase.Column_Id+" = ?";
		sdb.execSQL(deleteSql, new Object[]{id});
	}
	
	/**
	 * 查询配置列表
	 * @return
	 */
	public List<PlanStatementsBean> query(){
		List<PlanStatementsBean> list = null;
		String querySql = "select * from "+Config.DataBase.Table_PlanStatements+" where 1=1";
		Cursor cursor = sdb.rawQuery(querySql, null);
		if(cursor!=null){
			list = new ArrayList<PlanStatementsBean>();
			while(cursor.moveToNext()){
				PlanStatementsBean bean = new PlanStatementsBean();
				bean.setId(cursor.getLong(cursor.getColumnIndex(Config.DataBase.Column_Id)));
				bean.setTitle(cursor.getString(cursor.getColumnIndex(Config.DataBase.Column_Title)));
				bean.setPlanPay(cursor.getString(cursor.getColumnIndex(Config.DataBase.Column_PlanPay)));
				bean.setActualPay(cursor.getString(cursor.getColumnIndex(Config.DataBase.Column_ActualPay)));
				bean.setType(cursor.getString(cursor.getColumnIndex(Config.DataBase.Column_Type)));
				bean.setTimeFlag(cursor.getString(cursor.getColumnIndex(Config.DataBase.Column_TimeFlag)));
				list.add(bean);
			}
		}
		cursor.close();
		return list;
		
	}
	
	/**
	 * 根据ID查询具体配置项
	 * @param id
	 * @return
	 */
	public PlanStatementsBean queryById(String id){
		PlanStatementsBean bean = null;
		if(id!=null&&id.equals("")){
			String querySql = "select * from "+Config.DataBase.Table_PlanStatements+" where "+Config.DataBase.Column_Id+" = ?";
			Cursor cursor = sdb.rawQuery(querySql, new String[]{id});
			if(cursor!=null&&cursor.moveToNext()){
				bean.setId(cursor.getLong(cursor.getColumnIndex(Config.DataBase.Column_Id)));
				bean.setTitle(cursor.getString(cursor.getColumnIndex(Config.DataBase.Column_Title)));
				bean.setPlanPay(cursor.getString(cursor.getColumnIndex(Config.DataBase.Column_PlanPay)));
				bean.setActualPay(cursor.getString(cursor.getColumnIndex(Config.DataBase.Column_ActualPay)));
				bean.setType(cursor.getString(cursor.getColumnIndex(Config.DataBase.Column_Type)));
				bean.setTimeFlag(cursor.getString(cursor.getColumnIndex(Config.DataBase.Column_TimeFlag)));
			}
			cursor.close();
		}
		return bean;
		
	}
	
	public void close(){
		if(sdb!=null){
			sdb.close();
		}
	}

}
